package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.dto.TaskDailyDTO;
import com.haierp.enums.GeneralStatus;
import com.haierp.exception.ErpCommonException;
import com.haierp.mapper.TaskDailyDetailMapper;
import com.haierp.mapper.TaskDailyMapper;
import com.haierp.model.TaskDaily;
import com.haierp.model.TaskDailyDetail;
import com.haierp.service.ITaskDailyService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.PurchaseQueryVO;
import com.haierp.vo.TaskDailyVO;

@Service
public class TaskDailyServiceImpl extends SuperServiceImpl<TaskDailyMapper, TaskDaily>  implements ITaskDailyService {

	@Autowired
	private TaskDailyDetailMapper taskDailyDetailMapper;
	

	
	@Override
	public Integer queryTaskCount(TaskDailyDTO taskDailyDTO) {
		return this.baseMapper.queryTaskCount(taskDailyDTO);
	}

	@Override
	public List<TaskDailyVO> queryTaskDaily(TaskDailyDTO taskDailyDTO) {
		return this.baseMapper.queryTaskDaily(taskDailyDTO);
	}

	@Override
	public void addTaskDaily(TaskDaily taskDaily) {
		baseMapper.insert(taskDaily);
		if(taskDaily.getTaskDetailList()!=null&&!taskDaily.getTaskDetailList().isEmpty()){
			taskDaily.getTaskDetailList().forEach(
					taskDeatil->{
						taskDeatil.setTaskId(taskDaily.getId());
						taskDeatil.setCompanyId(taskDaily.getCompanyId());// 采购明细加入companyID
						taskDeatil.setStatus(GeneralStatus.INIT.getCode());
						taskDeatil.setGmtCreate(new Date());
						taskDeatil.setGmtModify(new Date());
						
						}
					);
			taskDailyDetailMapper.insertBatch(taskDaily.getTaskDetailList());
		}
	}

	@Override
	public TaskDaily query(Long id) {
		TaskDaily td = this.baseMapper.selectById(id);
		if(td!=null){
			List<TaskDailyDetail> tddl = taskDailyDetailMapper.queryDailyDetailByTaskId(id);
			if(tddl!=null){
				td.setTaskDetailList(tddl);
			}
		}
		return td;
	}

	@Override
	public void updateTaskDaily(TaskDaily taskDaily) {
		this.baseMapper.updateSelectiveById(taskDaily);
		List<TaskDailyDetail> tdList = taskDaily.getTaskDetailList();
		List<TaskDailyDetail> tdNew = new ArrayList<>(); //新增的SKU
		List<TaskDailyDetail> tdUpdate = new ArrayList<>();//修改的SKU
		//获取数据库里面的子任务
		TaskDaily tdDb = query(taskDaily.getId());
		List<TaskDailyDetail> tdListDb = tdDb.getTaskDetailList();
		Set<Long> all = new HashSet<Long>();
		if(tdListDb!=null&&!tdListDb.isEmpty()){
			tdListDb.forEach(t->{ all.add(t.getId()); });
		}
		tdList.forEach(t -> {
			if(t.getId()==null){
				t.setGmtCreate(new Date());
				t.setGmtModify(new Date());
				t.setStatus(GeneralStatus.INIT.getCode());
				t.setTaskId(taskDaily.getId());
				tdNew.add(t);
			}else{
				if(!all.isEmpty()){
					all.remove(t.getId());
				}
				t.setGmtModify(new Date());
				tdUpdate.add(t);
			}
		});
		if(!tdUpdate.isEmpty()){
			taskDailyDetailMapper.updateBatchById(tdUpdate);
		}
		if(!tdNew.isEmpty()){
			taskDailyDetailMapper.insertBatch(tdNew);
		}
		if(!all.isEmpty()){
			taskDailyDetailMapper.deleteBatchIds(new ArrayList<Long>(all));
		}
	}

	@Override
	public JsonPageResult<List<TaskDaily>> queryTaskDailyList(PurchaseQueryVO purchaseQueryVO) {
		JsonPageResult<List<TaskDaily>> taskDailyResult = new JsonPageResult<>();
		//1、查询总的记录数量
		Integer totalCount =  baseMapper.queryTaskDailyCount(purchaseQueryVO);
		
		//2、查询分页记录
		if(totalCount!=null&&totalCount!=0L){
			taskDailyResult.buildPage(totalCount, purchaseQueryVO);
			List<TaskDaily> tasks = baseMapper.queryTaskDailyList(purchaseQueryVO);
			taskDailyResult.setData(tasks);
		}
		return taskDailyResult;
	}
	
	@Override
	public void deleteTaskDailyById(Long id) {
		TaskDaily tjTaskDaily = new TaskDaily();
		tjTaskDaily.setId(id);
		TaskDaily taskDaily = this.baseMapper.selectOne(tjTaskDaily);
		if(taskDaily.getStatus() == 1) {
			throw new ErpCommonException("此采购状态不能删除，STATUS：" + GeneralStatus.of(taskDaily.getStatus()).getDescription());
		}
		this.baseMapper.deleteById(id);
		
		TaskDailyDetail taskDailyDetail = new TaskDailyDetail();
		taskDailyDetail.setTaskId(id);
		taskDailyDetailMapper.deleteSelective(taskDailyDetail);
	}
	
	@Override
	public void closeTaskDaily(List<Long> taskDailyIdList) {
		this.baseMapper.closeTaskDaily(taskDailyIdList);
		taskDailyDetailMapper.closeTaskDetailDaily(taskDailyIdList);
	}
	
	@Override
	public void autoCloseTaskDaily() {
		List<Long> taskDailyIdList = this.baseMapper.queryTaskDailyCloseIds();
		if(taskDailyIdList.size()>0) {
			this.baseMapper.closeTaskDaily(taskDailyIdList);
			taskDailyDetailMapper.closeTaskDetailDaily(taskDailyIdList);
		}
	}
	
	@Override
	public void finishTaskDaily(List<Long> taskDailyIdList) {
		this.baseMapper.finishTaskDaily(taskDailyIdList);
		taskDailyDetailMapper.finishTaskDetailDaily(taskDailyIdList);
		
	}
	
}
